package ma.myschool.app.dao.hibernate;

import java.util.List;

import ma.myschool.app.dao.MoisPayeDao;
import ma.myschool.app.model.MoisPaye;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;


/**
 * This class interacts with hibernate session to save/delete and
 * retrieve Role objects.
 *
 * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
 * @author jgarcia (updated to hibernate 4)
 */
@Repository
public class MoisPayeDaoHibernate extends GenericDaoHibernate<MoisPaye, Long> implements MoisPayeDao {

    /**
     * Constructor to create a Generics-based version using Role as the entity
     */
    public MoisPayeDaoHibernate() {
        super(MoisPaye.class);
    }

	@Override
	public MoisPaye getMoisPayeByLibelle(String libelle) {
		List moisPayes = getSession().createCriteria(MoisPaye.class).add(Restrictions.eq("libelle", libelle)).list();
        if (moisPayes.isEmpty()) {
            return null;
        } else {
            return (MoisPaye) moisPayes.get(0);
        }
	}

	@Override
	public List<MoisPaye> listMoisAt(MoisPaye moisPayeNow) {
		return getSession().createQuery("FROM MoisPaye m WHERE m.id <=" + moisPayeNow.getId()).list();
	}

	@Override
	public MoisPaye getMois(int i) {
		return (MoisPaye) getSession().createQuery("FROM MoisPaye m WHERE m.mois =" + i).uniqueResult();
	}
}
